Find the occurrence and position of the substringΒΆ
for match in re.finditer(p, S)
Find the occurrence and position of the
substrings within a string.
import re
S = 'Python exercises, PHP exercises, C# exercises'
pattern = 'exercises'
for match in re.finditer(pattern, S):
s = match.start()
e = match.end()
print('Found "%s" at %d:%d' % (text[s:e], s, e))
Output:
Found "exercises" at 7:16
Found "exercises" at 22:31
Found "exercises" at 36:45